<?php

namespace App\Http\Controllers\Front;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Services\PingAnPay\UnionApiOpen;
use App\Services\PingAnPay\UnionApiOpened;
use App\Services\PingAnPay\UnionApiSsms;
use App\Services\PingAnPay\UnionApiSubmit;

use App\Models\Order;

use Auth,Log;

class PingAnController extends Controller
{

    private $orderId;

    private $paydate;

    /**
     * [快捷支付-开通]
     * @param string $value [description]
     */
    public function unionApiOpen()
    {

        $UnionApiOpen = new UnionApiOpen();

        /*$subject = iconv('utf-8', 'gbk', $subject);
        Log::info('平安银行支付 order_id:'.$order_id);*/

        $user_id = Auth::user()->id;
        // $user_id = 'abcdefghijklmnopqrstuvwxyz1234';

        $UnionApiOpen->run($user_id);
    }


    /**
     * [快捷支付-已开通银行卡列表查询]
     * @param string $value [description]
     */
    public function unionApiOpened()
    {

        $return = ['status'=>1, 'message'=>'查询成功', 'data'=>''];

        $UnionApiOpen = new UnionApiOpened();

        $user_id = Auth::user()->id;
        // $user_id = 'abcdefghijklmnopqrstuvwxyz1234';

        $result = $UnionApiOpen->unionApiQuery($user_id);

        if ($result) {

            $results = json_decode($result);

            if ($results->body) {

                $return['data'] = $results->body;
            }else{

                $return['status'] = 0;

                $return['message'] = $results->errorMsg;

                return Response()->json($return);
            }
        }else{

            $return['status'] = 0;

            $return['message'] = '查询失败';

            return Response()->json($return);
        }

        return Response()->json($return);
    }



    /**
     * [快捷支付-获取支付短信验证码]
     * @param string $value [description]
     */
    public function unionApiSsms($order_id, $open_id = '20003111462016112845755004')
    {

        $return = ['status'=>1, 'message'=>'短信验证码发送成功', 'data'=>''];

        $orders = Order::find($order_id);

        if (empty($orders)) {

            $return['status'] = 0;

            $return['message'] = '订单信息不存在';

            return Response()->json($return);
        }

        $unionApiSsms = new UnionApiSsms();

        $user_id = Auth::user()->id;

        $this->orderId = config('pinganpay.masterId').date('Ymd').rand(10000000, 99999999);

        $this->paydate = date('YmdHis');

        $unionApiSsms->sendSms($this->orderId, $orders->pay_price, $this->paydate, $user_id, $open_id);

        return Response()->json($return);
    }


    public function unionApiSubmit($order_id, Request $request)
    {

        $return = ['status'=>1, 'message'=>'短信验证码发送成功', 'data'=>''];

        if ($request->orderId) {

            $orderId = $request->orderId;
        }else{

            $return['status'] = 0;

            $return['message'] = '订单号不能为空';

            return Response()->json($return);
        }

        if ($request->paydate) {

            $paydate = $request->paydate;
        }else{

            $return['status'] = 0;

            $return['message'] = '下单时间不能为空';

            return Response()->json($return);
        }

        if ($request->objectName) {

            $objectName = $request->objectName;
        }else{

            $return['status'] = 0;

            $return['message'] = '订单款项描述不能为空';

            return Response()->json($return);
        }

        if ($request->openId) {

            $openId = $request->openId;
        }else{

            $return['status'] = 0;

            $return['message'] = '开通银行卡Id不能为空';

            return Response()->json($return);
        }

        if ($request->verifyCode) {

            $verifyCode = $request->verifyCode;
        }else{

            $return['status'] = 0;

            $return['message'] = '短信验证码不能为空';

            return Response()->json($return);
        }

        if ($request->validtime) {

            $validtime = $request->validtime;
        }else{

            $validtime = 0;
        }


        $unionApiSubmit = new UnionApiSubmit();

        $orders = Order::find($order_id);

        if (empty($orders)) {

            $return['status'] = 0;

            $return['message'] = '订单信息不存在';

            return Response()->json($return);
        }

        $user_id = Auth::user()->id;
        // $user_id = 'abcdefghijklmnopqrstuvwxyz1234';

        $result = $unionApiSubmit->pay($user_id, $order_id, $orderId, $paydate, $orders->pay_price, $objectName, $openId, $verifyCode, $validtime);

        if ($result) {

            if ($result->status=='01') {      //status：01成功，02失败，00未成功

                $return['data'] = $result;
            }else{

                $return['status'] = 0;

                $return['message'] = $result->errorMsg;

                return Response()->json($return);
            }
        }else{

            $return['status'] = 0;

            $return['message'] = '订单支付失败';

            return Response()->json($return);
        }

        return Response()->json($return);
    }

}
